package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxPersonalModel;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.utils.ConstantHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2017/08/04
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController{

    @RequestMapping("/index")
    public ModelAndView index() {
        return new ModelAndView();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(WwdxPersonalModel model,
                                   @RequestParam(value = "page", defaultValue = "1") int page,
                                   @RequestParam(value = "rows",defaultValue = "10") int rows) {

        Map<String,Object> map = new HashMap<>(2);
        map.put("total", wwdxPersonalService.selectModelCount(model));
        List<WwdxPersonalModel> wwdxPersonalModels = wwdxPersonalService.selectModelList(model, page, rows);
        wwdxPersonalModels.forEach(wwdxPersonalModel -> {
            String key = String.format(ConstantHolder.USER_TOKEN, wwdxPersonalModel.getUnionId());
            wwdxPersonalModel.setToken((String) valueOperations.get(key));
        });
        map.put("rows", wwdxPersonalModels);
        return map;
    }

    /**
     * 发送邮件
     */
    @RequestMapping("/send")
    @ResponseBody
    public ResultBean send(String email) {
        boolean result = taskService.sendEmail(ConstantHolder.Constant.PRE_WWDXPERSONAL,email,"用户全量统计信息");
        return renderSuccess(result);
    }
}